package net.spider.tasks.sankaku;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;

import net.cl.utils.StringUtil;
import net.spider.Constants;
import net.spider.entity.ContentEntity;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class TrunkTask extends Thread {

	private static final String URL = "http://www.sankakucomplex.com/";
	private static final String PATTERN = "<a href=\"(\\S*)\">&raquo;</a>";
	private static final Log log = LogFactory.getLog(TrunkTask.class);
	private String currUrl;
	public static boolean END = false;
	public static final BlockingQueue<ContentEntity> QUEUE = StartUp.initTrunkQueue();

	public void run() {
		log.info("sankaku.TrunkTask started");
		currUrl = URL;
		while (currUrl != null)
			putQueue(getUrlList(currUrl));
		END = true;
		log.info("sankaku.TrunkTask end");
	}

	private List<ContentEntity> getUrlList(String url) {
		try {
			log.info(url);
			Document doc = Jsoup.connect(url).userAgent(Constants.USER_AGENT).timeout(20000).get();
			Elements e = doc.getElementById("posts").select("a[rel=bookmark]");
			List<ContentEntity> list = new ArrayList<ContentEntity>();
			for (Iterator<Element> iterator = e.iterator(); iterator.hasNext();) {
				Element element = iterator.next();
				ContentEntity entity = new ContentEntity();
				entity.setUrl(element.attr("href"));
				entity.setTitle(element.text());
				entity.setId(element.attr("id").substring(5));
				list.add(entity);
			}
			e = doc.getElementById("posts").select("div[class=wp-pagenavi");
			currUrl = StringUtil.getMatch(PATTERN, e.html());
			return list;
		} catch (IOException e) {
			log.error(url, e);
			return null;
		}
	}

	public static void putQueue(List<ContentEntity> list) {
		if (list != null)
			QUEUE.addAll(list);
	}

}
